package com.china08.yxyapi.api.home.activity.healthcompetition;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionQues;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudent;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudentAnswer;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionQuesRepository;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentAnswerRepository;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentRepository;

/**
 * 已答题集合
 * 
 * @author zd.Jin
 *
 *
 */
@RestController
@CrossOrigin
public class StudentQuesOrderLstController extends YxyApiBaseController {
	@Autowired
	private YxyCompetitionQuesRepository yxyCompetitionQuesRepository;
	@Autowired
	private YxyCompetitionStudentRepository yxyCompetitionStudentRepository;
	@Autowired
	private YxyCompetitionStudentAnswerRepository yxyCompetitionStudentAnswerRepository;

	@RequestMapping(value = "/api_yw/home/activity/healthcompetition/studentanswer/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
		RespModel respModel = new RespModel();
		YxyCompetitionStudent yxyCompetitionStudent = yxyCompetitionStudentRepository
				.findTop1ByStudentId(reqModel.studentId);
		if (yxyCompetitionStudent != null) {
			String stage = yxyCompetitionStudent.getStage();
			String stageno = "";
			if ("小学".equals(stage)) {
				respModel.wenjuan = "小学生健康教育知识竞赛试题";
				stageno = "01";
			} else if ("初中".equals(stage)) {
				respModel.wenjuan = "初高中学生健康教育知识竞赛试题";
				stageno = "02";
			} else if ("高中".equals(stage)) {
				respModel.wenjuan = "初高中学生健康教育知识竞赛试题";
				stageno = "03";
			}
			// 已答题目集合
			List<YxyCompetitionStudentAnswer> answers = yxyCompetitionStudentAnswerRepository
					.findByStudentIdOrderByQuesOrder(yxyCompetitionStudent.getStudentId());
			List<Question> questions = new ArrayList<>();
			for (int i = 1; i <= 100; i++) {
				YxyCompetitionQues yxyCompetitionQues = yxyCompetitionQuesRepository.findTop1ByStageAndQuesNo(stageno,
						i);
				Question question = new Question();
				question.questionOrder = i;
				if (yxyCompetitionQues != null) {
					question.quesAnswer = yxyCompetitionQues.getQuesAnswer();
					question.questionBody = yxyCompetitionQues.getBody();
					if (yxyCompetitionQues.getQuesOptions() != null) {
						question.quesOptions = yxyCompetitionQues.getQuesOptions().split("#");
					}
				}
				question.myAnswer = "";
				question.result = 1;
				for (YxyCompetitionStudentAnswer answer : answers) {
					if (answer.getQuesOrder() == i) {
						question.myAnswer = answer.getMyAnswer();
						if (answer.getQuesAnswer().equals(answer.getMyAnswer().substring(0, 1))) {
							question.result = 0;
						} else {
							question.result = 1;
						}
						break;
					}
				}
				questions.add(question);
			}
			respModel.questions = questions;

		}
		return respModel;
	}

	protected static class ReqModel extends YxyApiPageReqModel {

		public String studentId;// 学生id

		public void check() {
			Assert.notNull(StringUtils.defaultIfBlank(studentId, null), "学生id不能为空");
		}

	}

	protected static class RespModel extends YxyApiRespModel {
		// 问卷名称
		public String wenjuan;
		// 已答的题目集合
		public List<Question> questions;
	}

	// 题目回答情况实体类
	protected static class Question {
		// 题号
		public Integer questionOrder;
		// 状态(0已答1未答)
		public Integer status;
		// 答题结果(0回答正确1错误)
		public Integer result;

		// 我的答案
		public String myAnswer;
		// 正确答案
		public String quesAnswer;
		// 试题body
		public String questionBody;
		// 试题选项
		public String[] quesOptions;
	}
}
